//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "GeneralizedHullWhite.h"
using namespace Cephei::QL::Experimental::Shortrate;
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Lattice.h>
#include <gen/QL/TimeGrid.h>
#include <gen/QL/Models/CalibrationHelper.h>
#include <gen/QL/Math/Optimization/OptimizationMethod.h>
#include <gen/QL/Math/Optimization/EndCriteria.h>
#include <gen/QL/Math/Optimization/Constraint.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Models/ShortRateModel.h>
#include <gen/QL/Models/TermStructureConsistentModel.h>
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
using namespace Cephei::QL::Models;
using namespace Cephei::QL::Math::Optimization;
using namespace Cephei::QL::Math;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Shortrate::CGeneralizedHullWhite::CGeneralizedHullWhite (Cephei::QL::Termstructures::IYieldTermStructure^ yieldtermStructure, Cephei::Core::IVector<DateTime>^ speedstructure, Cephei::Core::IVector<DateTime>^ volstructure, Cephei::Core::IVector<Double>^ speed, Cephei::Core::IVector<Double>^ vol) : CTermStructureConsistentModel(CGeneralizedHullWhite::typeid)
{
    CYieldTermStructure^ _CyieldtermStructure;
    try
    {
#ifdef HANDLE
        _phGeneralizedHullWhite = NULL;
#endif
        _CyieldtermStructure = safe_cast<CYieldTermStructure^> (yieldtermStructure);
        _CyieldtermStructure->Lock();
        Handle<QuantLib::YieldTermStructure>& _yieldtermStructure = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CyieldtermStructure->GetHandle ()); 
        speedstructure->Lock();
        INativeVector<DateTime>^ _NCIspeedstructure = speedstructure->getFeature (NativeFeature::Value);
        CDateTimeVector^ _NCspeedstructure = safe_cast<CDateTimeVector^>(_NCIspeedstructure);
        std::vector<QuantLib::Date>& _speedstructure = static_cast<std::vector<QuantLib::Date>&> (_NCspeedstructure->GetReference ());
        volstructure->Lock();
        INativeVector<DateTime>^ _NCIvolstructure = volstructure->getFeature (NativeFeature::Value);
        CDateTimeVector^ _NCvolstructure = safe_cast<CDateTimeVector^>(_NCIvolstructure);
        std::vector<QuantLib::Date>& _volstructure = static_cast<std::vector<QuantLib::Date>&> (_NCvolstructure->GetReference ());
        speed->Lock();
        INativeVector<Double>^ _NCIspeed = speed->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCspeed = safe_cast<CDoubleVector^>(_NCIspeed);
        std::vector<QuantLib::Real>& _speed = static_cast<std::vector<QuantLib::Real>&> (_NCspeed->GetReference ());
        vol->Lock();
        INativeVector<Double>^ _NCIvol = vol->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCvol = safe_cast<CDoubleVector^>(_NCIvol);
        std::vector<QuantLib::Real>& _vol = static_cast<std::vector<QuantLib::Real>&> (_NCvol->GetReference ());
        _ppGeneralizedHullWhite = new boost::shared_ptr<QuantLib::GeneralizedHullWhite> (new QuantLib::GeneralizedHullWhite ( _yieldtermStructure,  _speedstructure,  _volstructure,  _speed,  _vol ));
        SetTermStructureConsistentModel (boost::dynamic_pointer_cast<QuantLib::TermStructureConsistentModel> (*_ppGeneralizedHullWhite));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CyieldtermStructure != nullptr) _CyieldtermStructure->Unlock();
        if (speedstructure != nullptr) speedstructure->Unlock();    //not optional
        if (volstructure != nullptr) volstructure->Unlock();    //not optional
        if (speed != nullptr) speed->Unlock();    //not optional
        if (vol != nullptr) vol->Unlock();    //not optional
    }
}
Cephei::QL::Experimental::Shortrate::CGeneralizedHullWhite::CGeneralizedHullWhite (Cephei::QL::Termstructures::IYieldTermStructure^ yieldtermStructure, Cephei::Core::IVector<DateTime>^ speedstructure, Cephei::Core::IVector<DateTime>^ volstructure) : CTermStructureConsistentModel(CGeneralizedHullWhite::typeid)
{
    CYieldTermStructure^ _CyieldtermStructure;
    try
    {
#ifdef HANDLE
        _phGeneralizedHullWhite = NULL;
#endif
        _CyieldtermStructure = safe_cast<CYieldTermStructure^> (yieldtermStructure);
        _CyieldtermStructure->Lock();
        Handle<QuantLib::YieldTermStructure>& _yieldtermStructure = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CyieldtermStructure->GetHandle ()); 
        speedstructure->Lock();
        INativeVector<DateTime>^ _NCIspeedstructure = speedstructure->getFeature (NativeFeature::Value);
        CDateTimeVector^ _NCspeedstructure = safe_cast<CDateTimeVector^>(_NCIspeedstructure);
        std::vector<QuantLib::Date>& _speedstructure = static_cast<std::vector<QuantLib::Date>&> (_NCspeedstructure->GetReference ());
        volstructure->Lock();
        INativeVector<DateTime>^ _NCIvolstructure = volstructure->getFeature (NativeFeature::Value);
        CDateTimeVector^ _NCvolstructure = safe_cast<CDateTimeVector^>(_NCIvolstructure);
        std::vector<QuantLib::Date>& _volstructure = static_cast<std::vector<QuantLib::Date>&> (_NCvolstructure->GetReference ());
        _ppGeneralizedHullWhite = new boost::shared_ptr<QuantLib::GeneralizedHullWhite> (new QuantLib::GeneralizedHullWhite ( _yieldtermStructure,  _speedstructure,  _volstructure ));
        SetTermStructureConsistentModel (boost::dynamic_pointer_cast<QuantLib::TermStructureConsistentModel> (*_ppGeneralizedHullWhite));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CyieldtermStructure != nullptr) _CyieldtermStructure->Unlock();
        if (speedstructure != nullptr) speedstructure->Unlock();    //not optional
        if (volstructure != nullptr) volstructure->Unlock();    //not optional
    }
}
Cephei::QL::Experimental::Shortrate::CGeneralizedHullWhite::CGeneralizedHullWhite (boost::shared_ptr<QuantLib::GeneralizedHullWhite>& childNative, Object^ owner) : CTermStructureConsistentModel(CGeneralizedHullWhite::typeid)
{
#ifdef HANDLE
	_phGeneralizedHullWhite = NULL;
#endif
	_ppGeneralizedHullWhite = &childNative;
    _ppTermStructureConsistentModel = new boost::shared_ptr<QuantLib::TermStructureConsistentModel> (boost::dynamic_pointer_cast<QuantLib::TermStructureConsistentModel> (*_ppGeneralizedHullWhite));
}
Cephei::QL::Experimental::Shortrate::CGeneralizedHullWhite::CGeneralizedHullWhite (QuantLib::GeneralizedHullWhite& childNative, Object^ owner) : CTermStructureConsistentModel(CGeneralizedHullWhite::typeid)
{
#ifdef HANDLE
	_phGeneralizedHullWhite = NULL;
#endif
	_ppGeneralizedHullWhite = new boost::shared_ptr<QuantLib::GeneralizedHullWhite> (&childNative);
    _ppTermStructureConsistentModel = new boost::shared_ptr<QuantLib::TermStructureConsistentModel> (boost::dynamic_pointer_cast<QuantLib::TermStructureConsistentModel> (*_ppGeneralizedHullWhite));
    _GeneralizedHullWhiteOwner = owner;
    _TermStructureConsistentModelOwner = owner;
}

Cephei::QL::Experimental::Shortrate::CGeneralizedHullWhite::CGeneralizedHullWhite (CGeneralizedHullWhite^ copy) : CTermStructureConsistentModel(CGeneralizedHullWhite::typeid)
{
#ifdef HANDLE
	_phGeneralizedHullWhite = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppGeneralizedHullWhite = new boost::shared_ptr<QuantLib::GeneralizedHullWhite> (copy->GetShared());
        _ppTermStructureConsistentModel = new boost::shared_ptr<QuantLib::TermStructureConsistentModel> (boost::dynamic_pointer_cast<QuantLib::TermStructureConsistentModel> (*_ppGeneralizedHullWhite));
    }
}
Cephei::QL::Experimental::Shortrate::CGeneralizedHullWhite::CGeneralizedHullWhite (PLATFORM::Type^ t) : CTermStructureConsistentModel(CGeneralizedHullWhite::typeid)
{
#ifdef HANDLE
	_phGeneralizedHullWhite = NULL;
#endif
	if (!t->IsSubclassOf(CGeneralizedHullWhite::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Shortrate::CGeneralizedHullWhite::CGeneralizedHullWhite (QuantLib::Handle<QuantLib::GeneralizedHullWhite>& childNative, Object^ owner)  : CTermStructureConsistentModel(CGeneralizedHullWhite::typeid)
{
	_phGeneralizedHullWhite = &childNative;
	_ppGeneralizedHullWhite = &static_cast<boost::shared_ptr<QuantLib::GeneralizedHullWhite>>(childNative.currentLink());
    _ppTermStructureConsistentModel = new boost::shared_ptr<QuantLib::TermStructureConsistentModel> (boost::dynamic_pointer_cast<QuantLib::TermStructureConsistentModel> (*_ppGeneralizedHullWhite));
    _GeneralizedHullWhiteOwner = owner;
}
Cephei::QL::Experimental::Shortrate::CGeneralizedHullWhite::CGeneralizedHullWhite (QuantLib::Handle<QuantLib::GeneralizedHullWhite> childNative)  : CTermStructureConsistentModel(CGeneralizedHullWhite::typeid)
{
	_phGeneralizedHullWhite = &childNative;
	_ppGeneralizedHullWhite = &static_cast<boost::shared_ptr<QuantLib::GeneralizedHullWhite>>(childNative.currentLink());
    _ppTermStructureConsistentModel = new boost::shared_ptr<QuantLib::TermStructureConsistentModel> (boost::dynamic_pointer_cast<QuantLib::TermStructureConsistentModel> (*_ppGeneralizedHullWhite));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Shortrate::CGeneralizedHullWhite::CGeneralizedHullWhite (QuantLib::GeneralizedHullWhite childNative)  : CTermStructureConsistentModel(CGeneralizedHullWhite::typeid)
{
#ifdef HANDLE
	_phGeneralizedHullWhite = NULL;
#endif
	_ppGeneralizedHullWhite = new boost::shared_ptr<QuantLib::GeneralizedHullWhite> (new QuantLib::GeneralizedHullWhite (childNative));
    _ppTermStructureConsistentModel = new boost::shared_ptr<QuantLib::TermStructureConsistentModel> (boost::dynamic_pointer_cast<QuantLib::TermStructureConsistentModel> (*_ppGeneralizedHullWhite));
}
#endif

Cephei::QL::Experimental::Shortrate::CGeneralizedHullWhite::~CGeneralizedHullWhite ()
{
    if (_ppGeneralizedHullWhite != NULL)
    {
	    delete _ppGeneralizedHullWhite;
        _ppGeneralizedHullWhite = NULL;
    }
}
Cephei::QL::Experimental::Shortrate::CGeneralizedHullWhite::!CGeneralizedHullWhite ()
{
    if (_ppGeneralizedHullWhite != NULL)
    {
	    delete _ppGeneralizedHullWhite;
    }
}
QuantLib::GeneralizedHullWhite& Cephei::QL::Experimental::Shortrate::CGeneralizedHullWhite::GetReference ()
{
    if (_ppGeneralizedHullWhite == NULL) throw REFNEW NativeNullException ();
	return **_ppGeneralizedHullWhite;
}
boost::shared_ptr<QuantLib::GeneralizedHullWhite>& Cephei::QL::Experimental::Shortrate::CGeneralizedHullWhite::GetShared ()
{
    if (_ppGeneralizedHullWhite == NULL) throw REFNEW NativeNullException ();
	return *_ppGeneralizedHullWhite;
}
QuantLib::GeneralizedHullWhite* Cephei::QL::Experimental::Shortrate::CGeneralizedHullWhite::GetPointer ()
{
    if (_ppGeneralizedHullWhite == NULL) throw REFNEW NativeNullException ();
	return &**_ppGeneralizedHullWhite;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::GeneralizedHullWhite>& Cephei::QL::Experimental::Shortrate::CGeneralizedHullWhite::GetHandle ()
{
	if (_phGeneralizedHullWhite == NULL)
	{
		_phGeneralizedHullWhite = new Handle<QuantLib::GeneralizedHullWhite> (*_ppGeneralizedHullWhite);
	}
	return *_phGeneralizedHullWhite;
}
#endif
bool Cephei::QL::Experimental::Shortrate::CGeneralizedHullWhite::HasNative () 
{
	return (_ppGeneralizedHullWhite != NULL);
}

Cephei::QL::ILattice^ Cephei::QL::Experimental::Shortrate::CGeneralizedHullWhite::Tree (Cephei::QL::ITimeGrid^ grid)
{
    CTimeGrid^ _Cgrid;
    try
    {
        _Cgrid = safe_cast<CTimeGrid^> (grid);
        _Cgrid->Lock();
        QuantLib::TimeGrid& _grid = static_cast<QuantLib::TimeGrid&> (_Cgrid->GetReference ()); 
    	boost::shared_ptr<QuantLib::Lattice> _rv = (boost::shared_ptr<QuantLib::Lattice>)(*_ppGeneralizedHullWhite)->tree ( _grid );   
        Cephei::QL::CLattice^ _nrv = REFNEW Cephei::QL::CLattice (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cgrid != nullptr) _Cgrid->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Shortrate::IGeneralizedHullWhite^ Cephei::QL::Experimental::Shortrate::CGeneralizedHullWhite_Factory::Create (Cephei::QL::Termstructures::IYieldTermStructure^ yieldtermStructure, Cephei::Core::IVector<DateTime>^ speedstructure, Cephei::Core::IVector<DateTime>^ volstructure, Cephei::Core::IVector<Double>^ speed, Cephei::Core::IVector<Double>^ vol)
{
    return REFNEW CGeneralizedHullWhite ( yieldtermStructure,  speedstructure,  volstructure,  speed,  vol);
}
Cephei::QL::Experimental::Shortrate::IGeneralizedHullWhite^ Cephei::QL::Experimental::Shortrate::CGeneralizedHullWhite_Factory::Create (Cephei::QL::Termstructures::IYieldTermStructure^ yieldtermStructure, Cephei::Core::IVector<DateTime>^ speedstructure, Cephei::Core::IVector<DateTime>^ volstructure)
{
    return REFNEW CGeneralizedHullWhite ( yieldtermStructure,  speedstructure,  volstructure);
}
